<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/10/8
  Time: 20:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>考试信息</title>
    <%@include file="../../common/header.jsp"%>
    <%@include file="../../common/ruoyi.jsp"%>

    <script>
        var layer;
        $(function () {
            layui.use('layer',function () {
                layer=layui.layer;
            });
        });

        function queryAll(pageNo){
            $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/exam.action?method=queryAll&pageNo="+pageNo,
                data:$("#course-form").serialize(),
                dataType:"json",
                success:function (msg) {
                    $("#tb_cou1").empty();
                    var list=msg.data;
                    for (var i = 0; i < list.length; i++) {
                        var state=null;
                        if (list[i].state==0){
                            state="启用";
                        } else {
                            state="禁用";
                        }
                        var tr="<tr>" +
                            "<td>"+list[i].id+"</td>"+
                            "<td>"+list[i].exam_name+"</td>"+
                            "<td>"+list[i].date+"</td>"+
                            "<td>"+state+"</td>"+
                            "<td>"+$("#op").html().replace('{cid}',list[i].id ).replace('{tid}',list[i].id ).replace('{csid}',list[i].id).replace('{emid}',list[i].id).replace('{ssid}',list[i].id).replace('{stid}',list[i].id)+"</td>"+
                            "</tr>";
                        $("#tb_cou1").append(tr);
                    }
                    pager = msg;
                    $("#sp").html("当前页:"+pager.pageNo+"/总页数:"+pager.pages);
                    $("#pageNo1").val(pager.pagerNo);
                    $("#pagerNo1").attr("max",pager.pages);
                },error:function(e){
                    alert(e);
                }
            });
        }

        //添加前初始化模态框表单
        function sub_add(id){
            $("#form1")[0].reset();
        }



        /**
         * 考试录入及修改
         */
        function add(){
            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=add",
                type:"post",
                data:$("#form1").serialize(),
                dataType:"json",
                success:function(data){
                    layer.msg("试卷添加或修改成功")
                    queryAll(1);
                },
                error:function (x,s,e) {
                    alert("错误："+s+e);
                }
            });

        }


        //修改前的查询
        function queryOne(id){

            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=queryOne&id="+id,
                type:"post",
                dataType:"json",
                success:function (data) {
                    $("#id").val(data.id);
                    $("#exam_name").val(data.exam_name);
                },error:function (e) {
                    alert(e);
                }
            });
        }


        //删除
        function exam_delete(id){
            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=delete&id="+id,
                type:"post",
                dataType:"json",
                success:function(data){
                    layer.msg("已删除试卷")
                    queryAll(1);
                },
                error:function (xhr,s,e) {
                    alert("错误："+xhr+s+e);
                }
            });
        }


        function updateState(id,state){
            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=updateState&id="+id+"&state="+state,
                type:"post",
                dataType:"json",
                success:function (data) {
                    layer.msg("状态已修改");
                    queryAll(1);
                },
                error:function (xhr,s,e) {
                    alert("错误："+xhr+s+e);
                }
            });
        }

        //录入前查询班级
        function queryClass(id){
            $("#form2")[0].reset();
            $("#class_id1").empty();
            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=queryClass",
                type:"post",
                dataType:"json",
                success:function (data) {
                    $("#exam_id").val(id);
                    $.each(data,function (i,e) {
                        $("#class_id1").append("<option value="+e.classid+" onclick=queryStu("+e.classid+","+id+")>"+e.classname+"</option>");

                    })
                },
                error:function (xhr,s,e) {
                    alert("错误："+xhr+s+e);
                }

            });
        }

        //根据班级查询学生
        function queryStu(classid,id){
            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=queryStu&classid="+classid+"&id="+id,
                type:"post",
                dataType:"json",
                success:function (msg) {
                    $("#tb_cou2").empty();
                    for (var i = 0; i < msg.length; i++) {
                        var tr = "<tr>" +
                            "<td>" +"<input type=\"text\" style=\"width:100px\" id=\"stuid\" name=\"stuid\"  readonly=\"readonly\"value="+msg[i].stuid+" />" + "</td>" +
                            "<td>" + "<input type=\"text\"   id=\"stuname\" name=\"stuname\"  readonly=\"readonly\"value="+msg[i].stuname+" />" + "</td>" +
                            "<td>" + "<input type=\"text\"  id=\"score\" name=\"score\" />"+ "</td>" +
                            "</tr>";


                            $("#tb_cou2").append(tr);


                    }
                },
                error:function (xhr,s,e) {
                    alert("错误："+xhr+s+e);
                }
            });
        }

        //批量提交成绩
        function add_score(){
            $.ajax({
                url:"${pageContext.request.contextPath}/exam.action?method=add_score",
                type:"post",
                data:$("#form2").serialize(),
                dataType:"json",
                success:function(data){
                    layer.msg("成绩录入成功");
                    queryAll(1);
                },
                error:function (xhr,s,e) {
                    alert("错误："+xhr+s+e);
                }
            });
        }

      $(function () {
          queryAll(1);
      })
    </script>
</head>
<body>
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <div class="col-sm-12 select-table table-striped">
                <input type="button" id="btn" class="btn btn-success" value="录入" onclick="sub_add()" data-toggle="modal" data-target="#myModal">
                <table class="table table-bordered table-hover">
                    <caption>考试信息</caption>
                    <thead>
                    <tr>
                        <td>编号</td>
                        <td>考试名称</td>
                        <td>考试时间</td>
                        <td>考试状态</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody id="tb_cou1">

                    </tbody>
                </table>


                <div id="page">
                    <ul class="pagination">
                        <li><span id="sp"></span></li>
                        <li><a href="javascript:void(0)" onclick="queryAll(1)">首页</a></li>
                        <li><a href="javascript:void(0)" onclick="queryAll(pager.prev)">上页</a></li>
                        <li><a href="javascript:void(0)" onclick="queryAll(pager.next)">下页</a></li>
                        <li><a href="javascript:void(0)" onclick="queryAll(pager.last)">尾页</a></li>
                        <li class="input-group">
                            <input type="number" value="" id="pageNo1" min="1"  step="1"/>
                            <input type="button" value="go" id="btnGo"/>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="op">
    <div class="hidden-sm hidden-xs action-buttons">
        <a class="green" href="#" onclick="queryOne({cid})" data-toggle="modal" data-target="#myModal">
            <i class="ace-icon fa fa-pencil "></i>修改
        </a>
        <a class="red" href="#" onclick="exam_delete({tid})">
            <i class="ace-icon fa fa-trash-o "></i>删除
        </a>
        <a class="red" href="#" onclick="queryClass({csid})" data-toggle="modal" data-target="#myModal1">
            <i class="ace-icon fa fa-trash-o "></i>成绩录入
        </a>
        <a class="red" href="../score/score.jsp?exam_id={emid}" >
            <i class="ace-icon fa fa-trash-o "></i>成绩查询
        </a>
        <select name="state" >
            <option value="0" onclick="updateState({stid},0)">启用</option>
            <option value="1" onclick="updateState({ssid},1)">禁用</option>
        </select>
    </div>
</script>


<!--模态框-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="z-index: 5000">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">课程</h4>
                </div>
                <div class="modal-body">
                    <%--类型录入修改--%>
                    <form role="form" id="form1" action="exam.action" >
                        <div class="form-group">
                            <%--编号--%>
                            <input type="hidden" class="form-control" id="id" name="id" />
                            <label for="exam_name">考试名称</label><input type="text" class="form-control " id="exam_name" name="exam_name" />
                        </div>
                        <div class="form-group">
                            <label for="date">考试时间</label><input type="date" class="form-control" id="date" name="date" />
                        </div>
                    </form>
                    <%--类型录入修改--%>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="btnSubmit" onclick="add()" data-toggle="modal" data-target="#myModal">提交更改</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="z-index: 5000">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel1">课程</h4>
                </div>
                <div class="modal-body">
                    <%--成绩录入--%>
                    <form role="form" id="form2" action="exam.action" >
                        <div class="form-group">
                            <%--试卷编号--%>
                            <input type="hidden" class="form-control" id="exam_id" name="exam_id" />
                            选择班级：<select name="class_id" id="class_id1"></select>
                            <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <td>学生编号</td>
                                            <td>学生姓名</td>
                                            <td>成绩</td>
                                        </tr>
                                    </thead>
                                    <tbody id="tb_cou2">

                                    </tbody>
                            </table>
                        </div>
                    </form>
                    <%--类型录入修改--%>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="btnSubmit1" onclick="add_score()" data-toggle="modal" data-target="#myModal1">提交成绩</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</body>
</html>
